/*将一般的JSON格式转为EasyUI TreeGrid树控件的JSON格式 
 * @param rows:json数据对象 
 * @param idFieldName:表id的字段名 
 * @param pidFieldName:表父级id的字段名 
 * @param fileds:要显示的字段,多个字段用逗号分隔 
 */  
 function ConvertToTreeGridJson(rows, idFieldName, pidFieldName, fileds) {  
     function exists(rows, ParentId) {  
         for (var i = 0; i < rows.length; i++) {  
             if (rows[i][idFieldName] == ParentId)  
                 return true;  
         }  
         return false;  
     }  
     var nodes = [];  
     // get the top level nodes  
     for (var i = 0; i < rows.length; i++) {  
         var row = rows[i];  
         if (!exists(rows, row[pidFieldName])) {  
             var data = {  
                 id: row[idFieldName]  
             }  
             var arrFiled = fileds.split(",");  
             for (var j = 0; j < arrFiled.length; j++)  
             {  
                 if (arrFiled[j] != idFieldName)  
                     data[arrFiled[j]] = row[arrFiled[j]];  
             }  
             nodes.push(data);  
         }  
     }  
  
     var toDo = [];  
     for (var i = 0; i < nodes.length; i++) {  
         toDo.push(nodes[i]);  
     }  
     while (toDo.length) {  
         var node = toDo.shift(); // the parent node  
         // get the children nodes  
         for (var i = 0; i < rows.length; i++) {  
             var row = rows[i];  
             if (row[pidFieldName] == node.id) {  
                 var child = {  
                     id: row[idFieldName]  
                 };  
                 var arrFiled = fileds.split(",");  
                 for (var j = 0; j < arrFiled.length; j++) {  
                     if (arrFiled[j] != idFieldName) {  
                         child[arrFiled[j]] = row[arrFiled[j]];  
                     }  
                 }  
                 if (node.children) {  
                     node.children.push(child);  
                 } else {  
                     node.children = [child];  
                 }  
                 toDo.push(child);  
             }  
         }  
     }  
     return nodes;  
 }
 
 
 function layer_edit(title,url,w,h,func){
	if (title == null || title == '') {
		title=false;
	};
	if (url == null || url == '') {
		url="404.html";
	};
	if (w == null || w == '') {
		w=800;
	};
	if (h == null || h == '') {
		h=($(window).height() - 50);
	};
	layer.open({
		type: 2,
		area: [w+'px', h +'px'],
		fix: false, //不固定
		maxmin: true,
		shade:0.4,
		title: title,
		content: url,
		btn: ['确定', '取消'] //只是为了演示
		,yes: function(){
			func(true);
		}
	    ,btn2: function(){
	    	func(false);
	    }
	});
}
 
function commonAjaxSubmit(url,formObj){
    if(!formObj||formObj==''){
        var formObj="form";
    }
    if(!url||url==''){
        var url=document.URL;
    }
    var loading=layer.open({type: 2});
    $(formObj).ajaxSubmit({
        url:url,
        type:"POST",
        success:function(data, st) {
            layer.close(loading);
            layer.open({
                content: data.msg,
                style: 'background-color:#000; color:#fff; border:none;',
                time: 2
            });
            if(data.url&&data.url!=''){
                setTimeout(function(){
                    top.window.location.href=data.url;
                },2000);
            }
            if(data.url==''){
                setTimeout(function(){
                    top.window.location.reload();
                },1000);
            }
        }
    });
    return false;
}
function get_form_data(){
    var arr=$("form").formToArray();
    var obj={};
    for(var i in arr){
        obj[arr[i].name]=arr[i].value;
    }
    return obj;
}

function commonPost(f,url){
    if(!url||url==''){
        var url=document.URL;
    }
    $.post(url,get_form_data(), function (data) {
        f(data);
    },'json');
}

function Hui_admin_custom_tab(href,title){
	if(href){
		var bStop = false,
			bStopIndex = 0,
			
			topWindow = $(window.parent.document),
			show_navLi = topWindow.find("#min_title_list li"),
			iframe_box = topWindow.find("#iframe_box");
			
		show_navLi.each(function() {
			if($(this).find('span').attr("data-href")==href){
				bStop=true;
				bStopIndex=show_navLi.index($(this));
				return false;
			}
		});
		if(!bStop){
			creatIframe(href,title);
			min_titleList();
		}
		else{
			show_navLi.removeClass("active").eq(bStopIndex).addClass("active");			
			iframe_box.find(".show_iframe").hide().eq(bStopIndex).show().find("iframe").attr("src",href);
		}
	}
}